package info.ericbutler.microcosm.game;

import info.ericbutler.microcosm.entities.ui.Item;
import info.ericbutler.microcosm.entities.ui.ItemStack;
import info.ericbutler.microcosm.enums.TileType;
import info.ericbutler.microcosm.world.GameSquare;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.Log;

public class LoadingState extends BasicGameState {
	private int	id;

	public LoadingState(int LOADING_STATE) {
		this.id = LOADING_STATE;
	}

	@Override
	public void init(GameContainer container, StateBasedGame game) throws SlickException {
		Log.info( "Loading main game font." );
		MicrocosmMain.GAME_FONT = new UnicodeFont( "rsc/images/fonts/6px2bus.ttf", 45, true, false );
		MicrocosmMain.GAME_FONT.getEffects().add( new ColorEffect( java.awt.Color.GRAY ) );
		MicrocosmMain.GAME_FONT.addAsciiGlyphs();
		MicrocosmMain.GAME_FONT.loadGlyphs();
		// Load map tile textures
		MicrocosmMain.TILES.put( "GRASS1", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/grass_01_16x16.png" ), 16, 16 ), 15000 ) );
		MicrocosmMain.TILES.put( "FANCY_GRASS1", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/fancy_grass_01_16x16.png" ), 16, 16 ), 15000 ) );
		MicrocosmMain.TILES.put( "TREE1", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/basic_tree_overlay_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.TILES.put( "TALL_GRASS1", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/tall_grass_01_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.TILES.put( "TALL_TREE1", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/tall_tree_01_16x48.png" ), 16, 48 ), 15000 ) );
		MicrocosmMain.TILES.put( "WOODEN_WALL_1_EW", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/wooden_wall_ew_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.TILES.put( "WOODEN_WALL_1_NS", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/wooden_wall_ns_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.TILES.put( "WOODEN_WALL_1_NE", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/wooden_wall_ne_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.TILES.put( "WOODEN_WALL_1_NW", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/wooden_wall_nw_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.TILES.put( "WOODEN_WALL_1_ES", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/wooden_wall_es_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.TILES.put( "WOODEN_WALL_1_SW", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/wooden_wall_sw_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.TILES.put( "WOODEN_WALL_1_NES", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/wooden_wall_nes_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.TILES.put( "WOODEN_WALL_1_NEW", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/wooden_wall_new_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.TILES.put( "WOODEN_WALL_1_NSW", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/wooden_wall_nsw_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.TILES.put( "WOODEN_WALL_1_ESW", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/wooden_wall_esw_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.TILES.put( "WOODEN_WALL_1_NESW", new Animation( new SpriteSheet( new Image( "rsc/images/terrain/wooden_wall_nesw_16x32.png" ), 16, 32 ), 15000 ) );
		MicrocosmMain.MENU_IMAGES.put( "inventory", new Image( "rsc/images/inventory_backdrop_230x200.png" ) );
		MicrocosmMain.MENU_IMAGES.put( "Rocks", new Image( "rsc/images/icons/rocks_1_16x16.png" ) );
		MicrocosmMain.MENU_IMAGES.put( "Logs", new Image( "rsc/images/icons/logs_1_16x16.png" ) );
		MicrocosmMain.MENU_IMAGES.put( "Wooden Wall 01", new Image( "rsc/images/icons/wooden_wall_1_16x16.png" ) );
		// Define Entity Templates
		Item.ROCKS = new Item( "Rocks", MicrocosmMain.MENU_IMAGES.get( "Rocks" ),
				new Animation( new SpriteSheet( "rsc/images/icons/rocks_1_16x16.png", 16, 16 ), 10000 ), 1, 16, false );
		Item.LOGS = new Item( "Logs", MicrocosmMain.MENU_IMAGES.get( "Logs" ), new Animation( new SpriteSheet( "rsc/images/icons/logs_1_16x16.png", 16, 16 ), 10000 ), 2,
				16, false );
		GameSquare.GRASS1 = new GameSquare( TileType.GRASS1, true, false, false, 0, null, 0, false ).setAnimation( MicrocosmMain.TILES.get( "GRASS1" ).copy() );
		GameSquare.TREE1 = new GameSquare( TileType.TREE1, false, true, true, 1, new ItemStack( Item.LOGS, 2 ), 15, false ).setAnimation( MicrocosmMain.TILES.get( "TREE1" ).copy() ).setOverlay( true );
		GameSquare.TALL_GRASS1 = new GameSquare( TileType.TALLGRASS1, true, true, false, 1, new ItemStack( Item.ROCKS, 1 ), 0, false ).setAnimation( MicrocosmMain.TILES.get( "TALL_GRASS1" ).copy() );
		GameSquare.TALL_TREE1 = new GameSquare( TileType.TALLTREE1, false, true, true, 2, new ItemStack( Item.LOGS, 4 ), 25, false ).setAnimation( MicrocosmMain.TILES.get( "TALL_TREE1" ).copy() );
		GameSquare.FANCY_GRASS1 = new GameSquare( TileType.FANCYGRASS1, true, false, false, 0, null, 5, false ).setAnimation( MicrocosmMain.TILES.get( "FANCY_GRASS1" ).copy() );
		GameSquare.WOODEN_WALL_1 = new GameSquare( TileType.WOODEN_WALL_01, false, false, true, 1, null, 0, true ).setAnimation( MicrocosmMain.TILES.get( "WOODEN_WALL_1_EW" ).copy() );
		Item.WOODEN_WALL_01 = new Item( "WOODEN_WALL_01", MicrocosmMain.MENU_IMAGES.get( "Wooden Wall 01" ), new Animation( new SpriteSheet(
				"rsc/images/terrain/wooden_wall_ew_16x32.png", 16, 16 ), 10000 ), 3, 16, true ).setWorldTile( GameSquare.WOODEN_WALL_1.clone() );
	}

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
		// TODO: add progress bar and calls to render throughout init
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
		Log.info( "Done initializing loading, entering Main Menu State" );
		game.enterState( MicrocosmMain.MAINMENU_STATE );
	}

	@Override
	public int getID() {
		return id;
	}
}
